﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Runtime.Serialization;


namespace Sencha.Direct
{
	public class RecordsActionResult<DataT> : DirectJsonResult
	{
		[DataContract]
		public class JsRecordsResponse
		{
			[DataMember(Name = "success")]
			public bool Success { get; set; }
			[DataMember(Name = "start")]
			public int Start { get; set; }
			[DataMember(Name = "limit")]
			public int Limit { get; set; }
			[DataMember(Name = "total")]
			public int Total { get; set; }
			[DataMember(Name = "records")]
			public IEnumerable<DataT> Items { get; set; }
		}

		new public JsRecordsResponse Data { get { return (JsRecordsResponse)base.Data; } set { base.Data = value; } }
		public override Type DataType { get { return typeof(JsRecordsResponse); } }

		public RecordsActionResult(IEnumerable<DataT> items, int? page, int? pageSize, int? total)
		{
			this.Data = new JsRecordsResponse
			{
				Items = items,
				Total = total ?? (items != null ? items.Count() : 0),
				Start = page ?? 0,
				Limit = pageSize ?? (items != null ? items.Count() : 0),
				Success = items != null
			};
		}

		public override string ToString()
		{
			return Convert.ToString(this.Data);
		}
	}
}
